//
//  UserInfoManager.m
//  much
//
//  Created by weijunjie on 2019/1/3.
//  Copyright © 2019年 iOS. All rights reserved.
//

#import "UserInfoManager.h"
#import "UICKeyChainStore.h"

@implementation UserInfoManager
iKY_AUTO_SERIALIZATION
+ (instancetype)instance {
    static UserInfoManager *onceObject;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        onceObject = [[UserInfoManager alloc] init];
    });
    return onceObject;
}
- (void)wakeupUser {
    // 获取用户帐号信息
    self.userBaseInfo = [NSKeyedUnarchiver unarchiveObjectWithData:[UICKeyChainStore dataForKey:UserInfoKey]];
}

- (void)logout:(void (^)(id _Nonnull))success {
    [MuchUserRequestManager userLoginOut:^(id  _Nonnull responseObject) {
        self.userBaseInfo = [[UserBaseInfo alloc] init];
        
        [NOTIFY_HANDLE postNotificationName:UserLogoutNotification object:nil];
    } failureBlock:^(NSString *error) {}];
}

- (void)setUserBaseInfo:(UserBaseInfo *)userBaseInfo {
    _userBaseInfo = userBaseInfo;
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:userBaseInfo];
    // 保存用户帐号信息
    [UICKeyChainStore setData:data forKey:UserInfoKey];
}


- (BOOL)isLogin {
    return self.userBaseInfo.token.hasValue;
}

@end

@implementation UserBaseInfo
iKY_AUTO_SERIALIZATION
@end
